package com.guigu.base.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.base.entity.BaseArea;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.base.feign.AddressFeign;
import com.guigu.base.mapper.AreaMapper;
import com.guigu.base.service.AreaService;
import com.guigu.tms.exception.TmsException;
import com.guigu.tms.result.ResultCode;
import com.guigu.user.entity.City;
import com.guigu.user.entity.Province;
import com.guigu.user.entity.vo.AreaVO;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-18
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, BaseArea> implements AreaService {
    @Autowired
    private AddressFeign addressFeign;

    /**
     * 获取当前行政信息
     *
     * @param provinceId
     * @param cityId
     * @param countyId
     * @return
     */
    @Override
    public BaseArea getCurrentAdministrativeAreaInformation(String provinceId, String cityId, String countyId) {
        StringBuilder address = new StringBuilder("");
        /**
         * 查询出省名
         */
        List<Province> allProvince = addressFeign.getAllProvince();
        String provinceName = "";
        for (Province province : allProvince) {
            if (province.getId().equals(provinceId)) {
                provinceName = province.getProvinceName();
                break;
            }
        }
        /**
         * 查询出城市名
         */
        List<City> allCity = addressFeign.getAllCity();
        String cityName = "";
        for (City city : allCity) {
            if (city.getId().equals(cityId)) {
                cityName = city.getCityName();
            }
        }

        /**
         * 查询出区县名
         */
        String areaName = "";
        List<AreaVO> allArea = addressFeign.getAllArea();
        for (AreaVO vo : allArea) {
            if (vo.getId().equals(countyId)) {
                areaName = vo.getAreaName();
            }
        }
        QueryWrapper<BaseArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", provinceName);
        /**
         * 查询出省名称
         */
        BaseArea baseArea = this.baseMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(baseArea)) {
            return null;
        }
        /**
         *连接起来
         */
        address.append(baseArea.getName());

        queryWrapper = new QueryWrapper<>();
        /**
         * 根据省id查询出它的市集合
         */
        queryWrapper.eq("parent_id", baseArea.getId());
        /**
         * 市集合
         */
        List<BaseArea> baseAreas = this.baseMapper.selectList(queryWrapper);
        if (CollUtil.isEmpty(baseAreas)) {
            return null;
        }
        /**
         * 市集合循环判断是否有我们这个市的值
         */
        for (BaseArea a : baseAreas) {
            /**
             * 有,则往下继续查找县集合
             */
            if (a.getName().equals(cityName)) {
                /**
                 * 将市连接起来
                 */
                address.append(a.getName());
                queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("parent_id", a.getId());

                List<BaseArea> list = this.baseMapper.selectList(queryWrapper);
                System.out.println("list = " + list);
                System.out.println("areaName = " + areaName);
                /**
                 * 根据县集合,查询出符合区域Id的行政数据
                 */
                for (BaseArea b : list) {
                    if (CollUtil.isEmpty(list)) {
                        return null;
                    }
                    if (b.getName().equals(areaName)) {
                        /**
                         * 将县也连接起来
                         */
                        address.append(b.getName());
                        b.setAddress(address.toString());
                        /**
                         * 全都找到了直接返回
                         */
                        return b;
                    }
                }
                /**
                 * 如果值找到了市和省,那么直接返回省+市
                 */
                a.setAddress(address.toString());
                return a;
            }
        }
        /**
         * 如果市和县都没有,那么则返回省
         */
        baseArea.setAddress(address.toString());
        return baseArea;
    }
}
